package com.example.testapp.activity.activity.study;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.activity.SuperActivity;
import com.example.testapp.activity.http.AsyncHttpUtil;
import com.example.testapp.activity.http.Contacts;
import com.example.testapp.activity.model.GameDatail;
import com.example.testapp.activity.model.GameDetailItem;
import com.example.testapp.activity.util.JsonUtils;
import com.example.testapp.activity.view.ErrorHintView;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.IllegalFormatCodePointException;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by l on 2016/11/22.
 */

public class GameDetailActivity extends SuperActivity {

    private ErrorHintView  mHintView;
    private LinearLayout   containLayout;
    private ImageView      mImageView;
    private TextView       mTitle;
    private TextView       mIntro;

    private List<GameDetailItem> mList = new ArrayList<>();
    private String ztid;

    public static int VIEW = 1;
    /**显示断网**/
    public static int VIEW_WIFIFAILUER = 2;
    /** 显示加载数据失败 **/
    public static int VIEW_LOADFAILURE = 3;
    public static int VIEW_LOADING = 4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_game_detail);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        ztid = intent.getStringExtra("ztid");

        RelativeLayout headView = (RelativeLayout) this.findViewById(R.id.head);
        headView.findViewById(R.id.back_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView title = (TextView) headView.findViewById(R.id.head_title);
        title.setText("游戏专题");
        mHintView = (ErrorHintView) this.findViewById(R.id.gamess_hintView);
        containLayout = (LinearLayout) this.findViewById(R.id.list);
        mTitle = (TextView) this.findViewById(R.id.name);
        mImageView = (ImageView) this.findViewById(R.id.image);
        mIntro = (TextView) this.findViewById(R.id.intro);

        showLoading(VIEW_LOADING);
        loadGameDetailData();
    }

    /**
     * 显示动态加载界面
     *
     * @param i
     */
    private void showLoading(int i) {
        mHintView.setVisibility(View.GONE);
        mImageView.setVisibility(View.GONE);
        mTitle.setVisibility(View.GONE);
        mIntro.setVisibility(View.GONE);
        containLayout.setVisibility(View.GONE);

        switch (i){
            case 1:
                mHintView.hideLoading();
                containLayout.setVisibility(View.VISIBLE);
                mImageView.setVisibility(View.VISIBLE);
                mTitle.setVisibility(View.VISIBLE);
                mIntro.setVisibility(View.VISIBLE);
                break;
            case 2:
                mHintView.hideLoading();
                mHintView.netError(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        showLoading(VIEW_LOADING);
                        loadGameDetailData();
                    }
                });
                break;
            case 3:
                mHintView.hideLoading();
                mHintView.loadFailure(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        showLoading(VIEW_LOADING);
                        loadGameDetailData();
                    }
                });
                break;
            case 4:
                mHintView.loadingdata();
                break;
        }
    }

    /**
     * 加载美女详情的图片
     */
    private void loadGameDetailData() {
        if (containLayout != null){
            containLayout.removeAllViews();
        }
        String url = Contacts.GAMES_URL_PATH + ztid;
        AsyncHttpUtil.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                  try {
                      if (responseBody != null && responseBody.length > 0){
                          JSONObject obj = new JSONObject(new String(responseBody));
                          JSONArray array = obj.getJSONArray("items");
                          GameDatail gameDatail = JsonUtils.getInstance(GameDatail.class, obj);
                          showLoading(VIEW);
                          if (gameDatail != null){
                              if (!TextUtils.isEmpty(gameDatail.getZtimg())){
                                  Picasso.with(mContext).load(Contacts.GAMES_ITEM_PATH + gameDatail.getZtimg()).placeholder(R.color.ECECEC).error(R.color.ECECEC).into(mImageView);
                              }
                              mTitle.setText(gameDatail.getZtname());
                              mIntro.setText(gameDatail.getIntro());
                          }
                          List<GameDetailItem> list = JsonUtils.getInstance(GameDetailItem.class, array);
                          if (list != null && list.size() > 0){
                              mList.clear();
                              mList.addAll(list);
                              addListView();
                          }
                      }else{
                          showLoading(VIEW_LOADFAILURE);
                      }
                  }catch (Exception e){
                      e.printStackTrace();
                      showLoading(VIEW_LOADFAILURE);
                  }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    showLoading(VIEW_WIFIFAILUER);
            }
        });
    }

    /**
     * 添加游戏列表
     */
    private void addListView() {
         for(int i = 0; i < mList.size(); i++){
           View view = View.inflate(mContext, R.layout.item_game_detail_lv, null);
             ImageView icon = (ImageView) view.findViewById(R.id.icon);
             TextView name = (TextView) view.findViewById(R.id.name);
             ImageView star = (ImageView) view.findViewById(R.id.star);
             TextView people = (TextView) view.findViewById(R.id.people);
             TextView download = (TextView) view.findViewById(R.id.download);
             TextView size = (TextView) view.findViewById(R.id.size);

             final GameDetailItem item = mList.get(i);
             if (!TextUtils.isEmpty(item.getIcon())){
                 Picasso.with(mContext).load(Contacts.GAMES_ITEM_PATH + item.getIcon()).placeholder(R.color.ECECEC).error(R.color.ECECEC).into(icon);
             }
             name.setText(item.getTitle());

             if (!TextUtils.isEmpty(item.getStar())){
                 setPraiseRate(star, item.getStar());
             }else{
                 star.setImageResource(R.mipmap.start5);
             }

             people.setText(item.getTotaldown() + "次");

             size.setText(item.getFilesize());

             download.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     Intent intent = new Intent(Intent.ACTION_VIEW);
                     Uri uri = Uri.parse(Contacts.GAMES_ITEM_PATH + item.getFlashurl());
                     intent.setData(uri);
                     startActivity(intent);
                 }
             });
             containLayout.addView(view);
           }
    }

    /**
     * 评分星级
     *
     * @param img
     * @param score
     */
    private void setPraiseRate(ImageView img, String score) {
       if (score.endsWith("0")){
           img.setImageResource(R.mipmap.start0);
       }else if (score.endsWith("0.5")){
           img.setImageResource(R.mipmap.start0_5);
       }else if (score.endsWith("1")){
           img.setImageResource(R.mipmap.start1);
       }else if (score.endsWith("1.5")){
           img.setImageResource(R.mipmap.start1_5);
       }else if (score.endsWith("2")){
           img.setImageResource(R.mipmap.start2);
       }else if (score.endsWith("2.5")){
           img.setImageResource(R.mipmap.start2_5);
       }else if (score.endsWith("3")){
           img.setImageResource(R.mipmap.start3);
       }else if (score.endsWith("3.5")){
           img.setImageResource(R.mipmap.start3_5);
       }else if (score.endsWith("4")){
           img.setImageResource(R.mipmap.start4);
       }else if (score.endsWith("4.5")){
           img.setImageResource(R.mipmap.start4_5);
       }else if (score.endsWith("5")){
           img.setImageResource(R.mipmap.start5);
       }else{
           img.setImageResource(R.mipmap.start5);
       }
    }

    @Override
    protected void obtainInfo() {

    }
}
